<template>
  <div class="app-container"
       v-loading.fullscreen.lock="allLoading"
       element-loading-text="拼命处理中,请耐心等待"
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-dialog :close-on-click-modal="changeBitFlagIsNone" :title="mainDialogInfo.title" :visible.sync="showDialog"
               width="700px" append-to-body @close="closeDialog">
      <el-form ref="form" :disabled="changeBitFlagIsNone" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="agent_ids" label="智能体作为工具">
          <SksSelectMultipleByLookupController
            :queryParams="{idNotIn:[form.id]}"
            request-url="/api/ai/ai-agent/lookup-sks-table-use-agent"
            :formData="form"
            prop-name="agent_ids"
            valueType="string"
            option-key="id" option-label="v_ai_model_info"
            :option-disabled-func="(item)=>item.status == 1"
          >
          </SksSelectMultipleByLookupController>
        </el-form-item>
        <el-form-item prop="tools" label="使用的工具">
          <SksSelectMultipleByLookupController
            :queryParams="{idNotIn:[form.id]}"
            request-url="/api/ai/ai-tools/lookup-sks-table-use-agent"
            :formData="form"
            prop-name="tools"
            valueType="string"
            option-key="id" option-label="tool_name"
            :option-disabled-func="(item)=>item.status == 1"
          >
          </SksSelectMultipleByLookupController>
        </el-form-item>
        <el-form-item prop="kb_id" label="知识库">
          <SksSelectSingleByLookupController
            :clearable="true"
            :queryParams="{}"
            request-url="/api/knowledge/lookup-sks-table-use-agent"
            :formData="form"
            prop-name="kb_id"
            valueType="number"
            option-key="id" option-label="name"
            @change="handleChangeKbId"
          >
          </SksSelectSingleByLookupController>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <template v-if="!changeBitFlagIsNone">
          <el-button type="primary" @click="submitMainDialog">确 定</el-button>
        </template>
        <el-button @click="cancelMainDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { sksDialogFormMixin } from 'sks-plugin-el-erp/lib/dialog-form'
import { mergeFromObject } from 'sks-plugin-el-erp/lib/core'
import { newSksTable } from 'sks-plugin-el-erp/lib/sks-model-utils'
import {
  addAIAgent,
  getAIAgent,
  lookupAIAgentUseAgent,
  lookupAIAgentUseChat,
  lookupAIModelUseAIConfig,
  updateAIAgent
} from '@/api/ai'

//用于选择模型
export default {
  name: "AIAgentSelectAgentsDialog",
  components: {
  },
  mixins: [
    sksDialogFormMixin,
  ],
  data() {
    return {
      defaultForm: {
        id: undefined,
        ai_model_id: undefined,
        name: undefined,
        description: undefined,
        emoji: undefined,
        tools: undefined,
        group: undefined,
        agent_ids: undefined,
        system_prompt: undefined,
        summarize_prompt: undefined,
        created_at: undefined,
        updated_at: undefined,
        kb_id: undefined,
      },
      defaultFormNotNull: {
        virtualDocumentList: [],
      },

      rules: {
      },
      lookupTable: mergeFromObject(newSksTable(),{
        queryParams:{
          pageNum: 1,
          pageSize: 9999,
          ai_api_id: undefined,
        },
      })
    };
  },
  methods:{
    showByUniqueId({uniqueId,extra,dialogInfo,virtualChangeBitFlag=this.sksUtils.CHANGE_BIT_FLAG.none}){
      getAIAgent({id:uniqueId}).then(res=>{
        console.log(res)
        let data=res.data
        this.show({
          extra: extra,
          form: this.sksUtils.mergeFromObject({},data,{
            virtualChangeBitFlag: virtualChangeBitFlag,
          }),
          dialogInfo: dialogInfo
        })
      })
    },
    validateSuccess() {
      let data = this.sksUtils.mergeFromObject({}, this.form);
      if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallback)) {
        this.mainDialogInfo.submitCallback({submitData: data, ref: this})
        //this.showDialog=false
        return
      }
      switch(data.virtualChangeBitFlag) {
        case this.sksUtils.CHANGE_BIT_FLAG.modify:
          updateAIAgent(data).then(res=>{
            this.sksUtils.msgSuccess("修改成功");
            this.closeDialog()
            if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallbackSuccess)){
              this.mainDialogInfo.submitCallbackSuccess({resData: data})
            }
          })
          break;
        default:
          this.sksUtils.msgError("未知的变更标识");
          break;
      }
    },
    handleChangeKbId({newVal,oldVal}={}){
     console.log('newVal,oldVal=',newVal,oldVal)
    },
  }
}
</script>

<style scoped>
</style>
